<?php

namespace app\common\model;

use think\Db;
use think\Model;

 /**
  * [Thirdparty 对接票务系统商品]
  * @Author   ZhouYunpeng
  * @DateTime 2020-04-27
  * @return   [type]      [description]
  */
class ThirdpartyGoods extends Model
{
    // 表名
    protected $name = 'thirdparty_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    

    public function getSystemList()
    {   

        $system =  Db::name('thirdparty_system')->select();
        $systemList =array_column($system,'system_name','id');
        return $systemList;
    }

    
    /**
     * [getScenicList 景区]
     * @Author   ZhouYunpeng
     * @DateTime 2020-04-28
     * @return   [type]      [description]
     */
    public function getScenicList()
    {   
        $scenic = Db::name('goods')
                  ->where('g.store_id',\think\Session::get('store.id'))
                  ->where('s.status',1)
                  ->where('s.delete_time','null')
                  ->alias('g')
                  ->join('scenic s','g.scenic_id = s.id','LEFT')
                  ->field('s.id,scenic_name')
                  ->select();
        $scenic_list =array_column($scenic,'scenic_name','id');
        return $scenic_list;
    }   

    /**
     * [getGoodsList 商品]
     * @Author   ZhouYunpeng
     * @DateTime 2020-04-28
     * @param    [type]      $thirdparty_id [description]
     * @return   [type]                     [description]
     */
    public function getGoodsList($thirdparty_id)
    {
      $thirdparty = Thirdparty::get($thirdparty_id);
      $goods   = Db::name('goods')->where(['scenic_id'=>$thirdparty['scenic_id'],'goods_status'=>1])->field('id,goods_name')->select();
      $goods_list =array_column($goods,'goods_name','id');
      return $goods_list;
    }

    /**
     * [bindGoods 绑定商品]
     * @Author   ZhouYunpeng
     * @DateTime 2020-04-28
     * @return   [type]      [description]
     */
    public function bindGoods($goods_id)
    {
       Db::name('goods')->where(['id'=>$goods_id])->update(['bind_thirdparty'=>1]);
    }

    /**
     * [delBind 删除绑定]
     * @Author   ZhouYunpeng
     * @DateTime 2020-04-28
     * @param    [type]      $goods_id [description]
     * @return   [type]                [description]
     */
    public function delBind($goods_id)
    {
      Db::name('goods')->where(['id'=>$goods_id])->update(['bind_thirdparty'=>0]);
    }
    
    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function thirdparty()
    {
        return $this->belongsTo('Thirdparty', 'thirdparty_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getPayModelList()
    {
        return ['0'=>'账户余额','2'=>'授信额度'];
    }
}
